package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppCompanyMapper;
import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.service.IAppCompanyService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.eco.fanliapp.utils.Convert;
import com.eco.fanliapp.utils.MapValueHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * 公司 服务层实现
 * 
 * @author xiaoxx
 * @date 2019-05-12
 */
@Service
public class AppCompanyServiceImpl  extends ServiceImpl<AppCompanyMapper, AppCompany> implements IAppCompanyService
{
	
	private static final Logger log = LoggerFactory.getLogger(AppCompanyServiceImpl.class);

	@Autowired
	private AppCompanyMapper appCompanyMapper;

	@Autowired
	private RedisTemplateService redisTemplateService;
	/**
     * 查询公司信息
     * 
     * @param companyId 公司ID
     * @return 公司信息
     */
    @Override
	public AppCompany selectAppCompanyById(Integer companyId)
	{
		return appCompanyMapper.selectAppCompanyById(companyId);
	}
	
	/**
     * 查询公司列表
     * 
     * @param appCompany 公司信息
     * @return 公司集合
     */
//	@Override
//	public List<AppCompany> selectAppCompanyList(AppCompany appCompany)
//	{
//	    return appCompanyMapper.selectAppCompanyList(appCompany);
//	}
	
    /**
     * 新增公司
     * 
     * @param appCompany 公司信息
     * @return 结果
     */
	@Override
	public int insertAppCompany(AppCompany appCompany)
	{
	    return appCompanyMapper.insertAppCompany(appCompany);
	}
	
	/**
     * 修改公司
     * 
     * @param appCompany 公司信息
     * @return 结果
     */
	@Override
	public int updateAppCompany(AppCompany appCompany)
	{
	    return appCompanyMapper.updateAppCompany(appCompany);
	}

	/**
     * 删除公司对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAppCompanyByIds(String ids)
	{
		return appCompanyMapper.deleteAppCompanyByIds(Convert.toStrArray(ids));
	}
	/**
	 * 根据用户ID查询分公司信息
	 * @param companyId
	 * @param type 0实时从数据库1优先从redis
	 * @return
	 */
	@Override
	public AppCompany findtAppCompany(Integer companyId,int type) {
		if (companyId == null) {
			companyId = 1;
		}
		String redisKeyStr = "app_company_info_" + companyId;
		AppCompany appCompany = null;
		if (type == 1 && redisTemplateService.hasKey(redisKeyStr)) {
			appCompany = redisTemplateService.getTByKey(redisKeyStr, AppCompany.class);
		}
		if (appCompany == null) {
			appCompany = appCompanyMapper.selectAppCompanyById(companyId);
			if (appCompany != null) {
				redisTemplateService.set(redisKeyStr, appCompany, 60 * 5);
			}
		}
		// TODO 查询分公司信息
		return appCompany;
	}

	/**
	 * 根据用户ID查询分公司信息
	 * @param userId
	 * @return
	 */
	@Override
	public AppCompany findtAppCompany(Long userId) {
		// TODO 查询分公司信息
		return appCompanyMapper.findtAppCompany(userId);
	}

	@Override
	public ServerResponse<PageInfo> findCommpanyFourALL(HashMap<String, Object> hashMap) {
		MapValueHelper mapValueHelper =  new MapValueHelper(hashMap);
		int currentPage = mapValueHelper.getInt("currentPage");
		int pageSize = mapValueHelper.getInt("pageSize");
		String keyword = mapValueHelper.getString("keyword");
		PageHelper.startPage(currentPage,pageSize);
		if(!StringUtils.isEmpty(keyword)){
			hashMap.put("keyword","%"+keyword+"%");
		}
		List<AppCompany> commpanyList = appCompanyMapper.findCommpanyFourALL(hashMap);
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("data",new PageInfo(commpanyList));
		return ServerResponse.createBySuccess("查询成功",new PageInfo(commpanyList));
	}

	@Override
	public List<AppCompany> findCompanys(Integer companyType) {
		HashMap map  = new HashMap();
		map.put("companyType",companyType);
		return appCompanyMapper.findCommpanyFourALL(map);
	}

	@Override
	public ServerResponse findCommpany(HashMap<String, Object> hashMap) {
		AppCompany appCompany = appCompanyMapper.findCommpanyFourALL(hashMap).get(0);
		return ServerResponse.createBySuccess(appCompany);
	}

	@Override
	public int updateCommpanyFour(HashMap<String, Object> hashMap) {
		/*MapValueHelper mapValueHelper =  new MapValueHelper(hashMap);
		int companyId = mapValueHelper.getInt("companyId");
		String taoBaoScheme = mapValueHelper.getString("taoBaoScheme");
		String generalFeeScheme = mapValueHelper.getString("generalFeeScheme");
		String orderFeeScheme = mapValueHelper.getString("orderFeeScheme");*/
		hashMap.put("upadateTime",System.currentTimeMillis());
		return appCompanyMapper.updateCommpanyFour(hashMap);

	}

	@Override
	public int insert(HashMap<String, Object> hashMap) {
		hashMap.put("createTime",System.currentTimeMillis());
		hashMap.put("upadateTime",System.currentTimeMillis());
		return appCompanyMapper.insert(hashMap);
	}

//	@Override
//	public AppCompany selectAppParentCompany() {
//		// TODO Auto-generated method stub
//		return appCompanyMapper.selectAppParentCompany();
//	}

	@Override
	public AppCompany findCompanyAll() {
		// TODO Auto-generated method findCompanyAll 查询当前总公司
		return appCompanyMapper.selectAppParentCompany();
	}

	@Override
	public AppCompany findtAppCompanyInviteCode(String string) {
		// TODO Auto-generated method stub 根据当前邀请码查询对应公司
		return appCompanyMapper.findtAppCompanyInviteCode(string);
	}

}
